/*
 * Created on 27-feb-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.juanfrivaldes.cio2005.gestion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.juanfrivaldes.cio2005.web.BaseAction;
import com.juanfrivaldes.cio2005.web.VerDocumentoAction;
import com.juanfrivaldes.cio2005.web.VerDocumentoForm;

/**
 * @author root
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class VerDocumentoGestionAction extends BaseAction {

	private final static String resumenesPath = "/cio2005-resumenes/";

	private final static String showResumenesPath = "/WEB-INF/files/resumenes/";


	private final static String finalesPath = "/cio2005-ponencias/";

	private final static String showFinalesPath = "/WEB-INF/files/ponencias/";
	
	
	private static Log log = LogFactory.getLog(VerDocumentoGestionAction.class);

	private void copyFile(String from, String to) throws IOException {
		FileInputStream stream = new FileInputStream(from);

		OutputStream bos = new FileOutputStream(to);
		int bytesRead = 0;
		byte[] buffer = new byte[8192];
		while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
			bos.write(buffer, 0, bytesRead);
		}
		stream.close();
		bos.close();

	}

	private boolean existsFile(String path) throws IOException {
		File file = new File(path);
		return file.isFile();
	}

	protected ActionForward protectedExecute(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (request.getParameter("id") != null) {

			String fileName = request.getParameter("id") + ".pdf";

			if(request.getParameter("tipo").equals("final")){
				String newFile = getServlet().getServletContext().getRealPath(
						showFinalesPath + fileName);

				if (!existsFile(newFile)) {
					log.trace("El fichero " + newFile + " no existe, lo creamos");
					copyFile(finalesPath + fileName, newFile);
				} else {
					log.trace("El fichero" + newFile + " ya existe, lo usamos");
				}
				log.trace("Escrito fichero " + newFile);
				return new ActionForward(showFinalesPath + fileName);
	
			}
			
			String newFile = getServlet().getServletContext().getRealPath(
					showResumenesPath + fileName);

			if (!existsFile(newFile)) {
				log.trace("El fichero " + newFile + " no existe, lo creamos");
				copyFile(resumenesPath + fileName, newFile);
			} else {
				log.trace("El fichero" + newFile + " ya existe, lo usamos");
			}
			log.trace("Escrito fichero " + newFile);
			return new ActionForward(showResumenesPath + fileName);

			}
						/*
			ActionForward forward = new ActionForward();
			forward.setRedirect(true);
			forward.setPath(showResumenesPath + fileName);
			return forward;
			*/
		else
			return mapping.findForward("success");
	}
}
